package com.ruoyi.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.domain.Activity;
import com.ruoyi.common.mapper.ActivityMapper;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.system.api.constant.YlStatus;
import com.ruoyi.system.service.IActivityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.BatchResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商家活动表，存储商家发布的霸王餐活动信息 服务实现类
 * </p>
 *
 * @author jiabao
 * @since 2025-06-25
 */
@Service
@Slf4j
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements IActivityService {

    @Resource
    private RedisService redisService;


    @Override
    public List<Activity> selectActivityList(Activity activity) {
        Long merchantId = activity.getMerchantId();
        LambdaQueryWrapper<Activity> query = Wrappers.<Activity>lambdaQuery();
        query.eq(Activity::getStatus, YlStatus.NORMAL.getCode());
        query.eq(ObjectUtils.isNotEmpty(merchantId), Activity::getMerchantId, merchantId);
        query.orderByDesc(Activity::getCreatedAt);
        return this.getBaseMapper().selectList(query);
    }

    @Override
    public Activity selectActivityById(Long id) {
        return this.getBaseMapper().selectById(id);
    }

    @Override
    public int insertActivity(Activity activity) {
        return this.getBaseMapper().insert(activity);
    }

    @Override
    public int updateActivity(Activity activity) {
        return this.getBaseMapper().updateById(activity);
    }

    @Override
    public int deleteActivityByIds(Long[] ids) {
        List<Activity> list = this.getBaseMapper().selectByIds(CollectionUtils.toList(ids));
        list.forEach(e -> e.setStatus(YlStatus.ABNORMAL.getCode()));
        List<BatchResult> batchResults = this.getBaseMapper().updateById(list);
        return 1;
    }


}
